﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace COSIX
{
    static class StringExtensions
    {
        public static bool StartsWith_(this string s, char c)
        {
            return s[0] == c;
        }

        public static bool StartsWith_(this string s, string s1)
        {
            bool a = false;
            for (int i = 1; i < s1.Length; i++)
            {
                if (s[i - 1] == s1[i - 1])
                {
                    a = true;
                    continue;
                }
                else
                {
                    a = false;
                    break;
                }
            }           
            return a;
        }

        /// <summary>
        /// Splits the string at the index where first white space is found.
        /// </summary>
        /// <param name="source">the source string</param>
        /// <returns>splitted string array.</returns>
        public static String[] SplitAtFirstSpace(this String source)
        {
            if (String.IsNullOrEmpty(source)) return new String[] { };

            var index = source.IndexOf(' ');

            if (index == -1) return new String[] { source };

            return new String[] { source.Substring(0, index), source.Substring(index + 1) };
        }

        /// <summary>
        /// Compares two instances of string.
        /// </summary>
        /// <param name="source">the source string</param>
        /// <param name="target">the other instance.</param>
        /// <returns>true if the two instances are considered equal, false otherwise.</returns>
        public static bool IsEqual(this String source, String target)
        {
            if (source == null && target == null) return true; // both instances are null.

            if (String.IsNullOrEmpty(source) && String.IsNullOrEmpty(target)) return true; // both instances are empty.

            return source.ToLower() == target.ToLower();
        }
    }
}
